from socket import socket, SOCK_STREAM, AF_INET
from base64 import b64encode
from json import dumps
from threading import Thread


def main():
    class FileTransferHandler(Thread):
        def __init__(self, cclient):
            super().__init__()
            self.cclient = cclient

        def run(self):
            my_dict = {'filename': 'test.png', 'filedata': data}
            json_str = dumps(my_dict)
            self.cclient.send(json_str.encode('utf-8'))
            self.cclient.close()

    # 1.创建套接字
    server = socket()
    # 2.绑定端口与ip地址与端口
    server.bind(('127.0.0.1', 5566))
    # 3.开启监听  监听消息发送
    server.listen(512)
    print('the listening is starting .......')
    with open('R-C.png', 'rb') as f:
        data = b64encode(f.read()).decode()
    while True:
        client, addr = server.accept()
        FileTransferHandler(client).start()


if __name__ == '__main__':
    main()
